import React from 'react'
import store from '../../store/store'
import { incrementAction, decrementAction,waitcrementAction } from '../../store/actions/countAction'
export default function Count() {
  //拿到store的值
  const { count } = store.getState().count
  //因为合并了多个reducer 
  //console.log(store.getState(),'store.getState()');

  //console.log(store.getState(),'store.getState()');
  //这里拿到的是仓库里的设置的对象


  //累加
  const incrementHandle = () => {
    //通过dispatch方法 向仓库传递 仓库接受到后会调用reducer 然后返回一个新的状态
    store.dispatch(incrementAction(1))
  }

  //累减
  const decreamentHandle = () => {
    store.dispatch(decrementAction(1))
  }

  //奇数再加
  const ifOddDecrementHandle = () => {
    if (count % 2 !== 0) {
      store.dispatch(incrementAction(1))
    }
  }

  //等2s再加
  const waitDecrementHandle = () => {
    store.dispatch(waitcrementAction(1))
  }

  return (
    <div>
      <h1>我是Count组件 我的值是{count}</h1>
      <div>
        <button onClick={incrementHandle}>累加</button>
        <button onClick={decreamentHandle}>累减</button>
        <button onClick={ifOddDecrementHandle}>如果是奇数再加1</button>
        <button onClick={waitDecrementHandle}>等2s再加1</button>
      </div>

    </div>
  )
}
